home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ For TASM / USRGUIDE.PAK / COUNTLG.ASM < prev    next >
Assembly Source File  |  1996-02-21  |  3KB  |  59 lines

  1. ; Turbo Assembler example. Copyright (c) 1993 By Borland International, Inc.
  2.  
  3. ; COUNTLG.ASM
  4. ; Large model C++-callable assembler function to count the number
  5. ; of lines and characters in a zero-terminated string.
  6. ;
  7. ; Function prototype:
  8. ;       extern unsigned int LineCount(char * far StringToCount,
  9. ;              unsigned int * far CharacterCountPtr);
  10. ;       char far * StringToCount: pointer to the string on which
  11. ;                                 a line count is to be performed
  12. ;
  13. ;       unsigned int far * CharacterCountPtr: pointer to the int variable
  14. ;                                             in which the character count
  15. ;                                             is to be stored
  16. ;
  17. ; Usage: bcc -ml callct.cpp countlg.asm
  18. ;
  19. ; From the Turbo Assembler User's Guide. 
  20. ; Ch. 18: Interfacing Turbo Assembler with Borland C++
  21.  
  22. NEWLINE  EQU     0ah                  ;the linefeed character is C's newline
  23.                                       ; character
  24.          .MODEL  LARGE
  25.          .CODE
  26.          PUBLIC  _LineCount
  27. _LineCount       PROC
  28.          push    bp
  29.          mov     bp,sp
  30.          push    si                   ;preserve calling program's register
  31.                                       ; variable, if any
  32.          push    ds                   ;preserve C's standard data seg
  33.          lds     si,[bp+6]            ;point DS:SI to the string
  34.          sub     cx,cx                ;set character count to 0
  35.          mov     dx,cx                ;set line count to 0
  36. LineCountLoop:
  37.          lodsb                        ;get the next character
  38.          and      al,al               ;is it null, to end the string?
  39.          jz       EndLineCount        ;yes, we're done
  40.          inc      cx                  ;no, count another character
  41.          cmp      al,NEWLINE          ;is it a newline?
  42.          jnz      LineCountLoop       ;no, check the next character
  43.          inc      dx                  ;yes, count another line
  44.          jmp      LineCountLoop
  45. EndLineCount:
  46.          inc      dx                  ;count line ending with null character
  47.          les      bx,[bp+10]          ;point ES:BX to the location at
  48.                                       ; which to return character count
  49.          mov      es:[bx],cx          ;set the character count variable
  50.          mov      ax,dx               ;return the line count as
  51.                                       ; the function value
  52.          pop      ds                  ;restore C's standard data seg
  53.          pop      si                  ;restore calling program's
  54.                                       ; register variable, if any
  55.          pop      bp
  56.          ret
  57. _LineCount        ENDP
  58.          END
  59.